<template>
  <div class="login">
    <div class="content">
      <div class="logo">
        <img src="../assets/logo.png" alt="" srcset="">
      </div>
      <el-form :model="formInline" class="demo-form-inline form" ref="formIn" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="formInline.username" class="login_user" prefix-icon="el-icon-user"
            placeholder="账号">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formInline.password" class="login_password"
            prefix-icon="el-icon-message-solid" placeholder="******" type="password">
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="enter">
            登录</el-button>
          <el-button type="info" @click="rest">重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LogIn',
  data() {
    return {
      formInline: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入账号名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入账号密码', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    rest() {
      this.$refs.formIn.resetFields()
      this.$message('已重置账号密码')
    },
    enter() {
      // 表单验证是否成功
      this.$refs.formIn.validate(async (val) => {
        if (!val) {
          this.$message.error('抱歉，账号或密码不符合格式')
          return
        }
        const { data: res } = await this.$http.post('login', this.formInline)
        // console.log(res)
        if (res.meta.status == 200) {
          // 检查用户管理状态
          // 会话存储用户的token值
          window.sessionStorage.token = res.data.token
          this.$message({
            message: '登录成功，欢迎回来！',
            type: 'success',
          })
          this.$router.push('/home')
        } else {
          this.$message.error('抱歉，登录失败')
        }
      })
    },
  },
}
</script>

<style lang='less' scoped>
.login {
  background: url('../assets/bg.jpg') no-repeat center / 100% 100%;
  width: 100%;
  height: 100vh;
  position: relative;
}
.content {
  background: #ffffff80;
  width: 600px;
  height: 450px;
  border-radius: 6px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  backdrop-filter: blur(10px);
}
.logo {
  width: 140px;
  height: 140px;
  border-radius: 100%;
  padding: 5px;
  position: absolute;
  background: #e5e1e1;
  text-align: center;
  top: -50%;
  left: 50%;
  transform: translate(-50%, 80%);
  border: 15px solid #fff;
  img {
    width: 140px;
    height: 140px;
    border-radius: 100%;
  }
}
.form {
  position: absolute;
  width: 100%;
  height: 220px;
  bottom: 0;
  margin-bottom: 50px;
  padding: 0 30px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>